<?php

class LocalityNameController extends GxController {

    public function filters() {
        return array('rights');
    }

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id, 'LocalityName'),
        ));
    }

    public function actionCreate() {
        $model = new LocalityName;
        if (isset($_POST['LocalityName'])) {
            $model->setAttributes($_POST['LocalityName']);
            if ($model->save()) {
                LocalityName::getlocaltyLatLogn($model->id, $model->locality_name);
                if (Yii::app()->getRequest()->getIsAjaxRequest())
                    Yii::app()->end();
                else
                    $this->redirect(array('view', 'id' => $model->id));
            }
        }

        $this->render('create', array('model' => $model));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id, 'LocalityName');
        $url = 'localityName/admin';
        $text = 'Locality';
        if (isset($_POST['LocalityName'])) {
            $model->setAttributes($_POST['LocalityName']);
            if ($model->save()) {
                LocalityName::getlocaltyLatLogn($model->id, $model->locality_name);
                $this->redirect(array('view', 'id' => $model->id));
            }
        }

        $this->render('update', array(
            'model' => $model, 'url' => $url, 'text' => $text
        ));
    }

    public function actionupdatelocalityname($id, $type) {
        $model = $this->loadModel($id, 'LocalityName');
        $url = '';
        $text = '';
        if ($type != 'class') {
            $url = 'Courses/admin';
            $text = 'Courses';
        } elseif ($type == 'class') {
            $url = 'classes/admin';
            $text = 'Classes';
        }
        $localityName = $model->locality_name;
        if (isset($_POST['LocalityName'])) {
            $criteria = new CDbCriteria;
            $criteria->select = 'id,venue_locality';  // only select the 'title' column
            $criteria->condition = 'users_id=:users_id AND venue_locality=:venue_locality';
            $criteria->params = array(':users_id' => $model->users_id, ':venue_locality' => trim($localityName));
            $changeLocality = ClassVenue::model()->find($criteria); // $params is not needed 
            ClassVenue::model()->updateByPk($changeLocality->id, array('venue_locality' => $_POST['LocalityName']['locality_name']));
            $updateLocality = LocalityName::model()->findAllByAttributes(array('locality_name' => $_POST['LocalityName']['locality_name']));
            if (!empty($updateLocality)) {
                LocalityName::model()->deleteByPk($model->id);
            } else {
                $model->setAttributes($_POST['LocalityName']);
                $model->save();
                LocalityName::getlocaltyLatLogn($model->id, $model->locality_name);
            }
            if ($type != 'class') {
                $this->redirect(array('Courses/admin'));
            } elseif ($type == 'class') {
                $url = 'classes/admin';
                $this->redirect(array('classes/admin'));
            }
        }
        $this->render('update', array(
            'model' => $model, 'url' => $url, 'text' => $text
        ));
    }

    public function actionDelete($id) {
        if (Yii::app()->getRequest()->getIsPostRequest()) {
            $this->loadModel($id, 'LocalityName')->delete();

            if (!Yii::app()->getRequest()->getIsAjaxRequest())
                $this->redirect(array('admin'));
        }
        else
            throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
    }

    public function actionAdmin() {
        $model = new LocalityName('search');
        $model->unsetAttributes();
        if (isset($_GET['LocalityName']))
            $model->setAttributes($_GET['LocalityName']);
        $statuschangeurl = Yii::app()->createUrl('localityName/statuschange/');
        $this->render('admin', array(
            'model' => $model, 'statuschangeurl' => $statuschangeurl
        ));
    }

    public function actiongetAlldata() {
        ini_set('max_execution_time', 300);
        $all = LocalityName::model()->findAll('id>=475');
        $i = 1;
        foreach ($all as $data) {
            $address = $data->locality_name;
            $status = LocalityName::getlocaltyLatLogn($data->id, $address);
            echo $i . ' id ' . $data->id . ' Status' . $status . '  <br>';
        }
        $i++;
        echo 'ok';
    }

    public function actionStatuschange() {
        $model = $this->loadModel($_POST['id'], 'LocalityName');
        $model->status = $_POST['status'];
        $model->save();
    }

    public function actiongetLocality() {
        $Locality = array();
        $data = Yii::app()->db->createCommand()
                ->select('*')
                ->from('skill_locality_name')
                ->where('locality_name LIKE  "%' . $_GET['term'] . '%"')
                ->order('locality_name ASC')
                ->queryAll();
        foreach ($data as $value) {
            $Locality[] = trim($value['locality_name']);
        }
        if (!empty($Locality)) {
            echo CJSON::encode($Locality);
        } else {
            echo SkillConfig::STATUS_DEACTIVE;
        }
    }

    public function actionsuggestlocalityname($usersid, $type) {
        $dataProvider = new CActiveDataProvider('LocalityName', array(
            'criteria' => array(
                'condition' => 'users_id=' . $usersid . ' AND status=' . SkillConfig::STATUS_DEACTIVE,
            ),
            'pagination' => array(
                'pageSize' => 20,
            ),
        ));
        $this->render('suggestlocalityname', array(
            'model' => $dataProvider, 'users_id' => $usersid, 'type' => $type
        ));
    }

}